package com.cn.bcyp.api.utils;

import java.io.Closeable;
import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;

public class HttpClientGet {

	public static String doGetCookie(String url, String cookie) {
		String httpStr = "";
		HttpGet httpGet = new HttpGet(url);
		CookieStore cookieStore = new BasicCookieStore();
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		try {
			httpGet.addHeader(new BasicHeader("Cookie", cookie));
			HttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			httpStr = EntityUtils.toString(entity, "UTF-8");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				((Closeable) httpClient).close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return httpStr;
	}
	
	
	public static Header[] doGetHeader(String Url) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		Header[] header = null;
		HttpGet httpGet = new HttpGet(Url);
		try {
			HttpResponse response = httpClient.execute(httpGet);
			header = response.getAllHeaders();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				((Closeable) httpClient).close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return header;
	}
	
	
	public static Header[] doGetHeader(String Url, String cookie) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		Header[] header = null;
		HttpGet httpGet = new HttpGet(Url);
		try {
			httpGet.addHeader(new BasicHeader("Cookie", cookie));
			HttpResponse response = httpClient.execute(httpGet);
			header = response.getAllHeaders();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			// 关闭连接,释放资源
			try {
				((Closeable) httpClient).close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return header;
	}
	
	
	
}
